package com.zx._01_基础._013_io;

import com.zx.common.Constant;
import com.zx.common.util.OSUtil;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import junit.framework.TestCase;

public class Demo_03_Writer extends TestCase {

    // 字节流写数据
    public void test1() throws IOException {
        final InputStream is = new FileInputStream(Constant.io_read_text);
        final OutputStream os = new FileOutputStream(Constant.io_write_text);

        final byte[] b = new byte[10];
        int len = 0;

        while ((len = is.read(b)) != -1) {
            os.write(b, 0, len);
        }
        os.close();
        is.close();
    }

    // 字符流写数据
    public void test2() throws IOException {
        final Reader re = new FileReader(Constant.io_read_text);
        final Writer wr = new FileWriter(Constant.io_write_text);
        final char[] c = new char[10];
        int len = 0;

        while ((len = re.read(c)) != -1) {
            wr.write(c, 0, len);
        }
        wr.close();
        re.close();
    }

    public void test3() throws IOException {
        final Writer wr = new FileWriter(Constant.io_write_text);
        wr.write("1234" + OSUtil.getSeparator());
        wr.write("测试" + OSUtil.getSeparator());
        wr.write("ad" + OSUtil.getSeparator());
        wr.write("阿大声道" + OSUtil.getSeparator());
        wr.close();
    }
}
